import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import csv

filename=r'F:\AI\data\bikesharing.csv'
df=pd.read_csv(filename)
y=df['casual']
x=df.drop('casual',axis=1)
feat_names=x.columns

categortcal_features=['season','mnth','weathersit','weekday']
x[categortcal_features].astype('object')
x_cat=x[categortcal_features]
x_cat=pd.get_dummies(x_cat)

from sklearn.preprocessing import StandardScaler
ss_x=StandardScaler()
ss_y=StandardScaler()
x=ss_x.fit_transform(x)
y=ss_y.fit_transform(y.reshape(-1,1))
fe_data=pd.DataFrame(data=x,index=df.inex)
fe_data=pd.concat([fe_data,x_cat],axis=1,ignore_index=False)
fe_data['casual']=y


from sklearn.model_selection import  train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,random_state=33,test_size=0.2)

from sklearn.linear_model import LinearRegression
lr=LinearRegression()
lr.fit(x_train,y_train)
y_test_pred_lr=lr.predict(x_test)
y_train_pred_lr=lr.predict(x_train)
fs=pd.DataFrame({'columns':list(feat_names),'coef':list((lr.coef_.T))})
fs.sort_values(by=['coef'],ascending=False)

from sklearn.linear_model import RidgeCV
alphas=[0.01,0.1,1,10,100]
ridge=RidgeCV(alphas=alphas,store_cv_values=True)
ridge.fit(x_train,y_train)
y_test_pred_ridge=lr.predict(x_test)
y_train_pred_ridge=lr.predict(x_train)

from sklearn.linear_model import LassoCV
alphas=[0.01,0.1,1,10,100]
lasso=LassoCV(alphas=alphas,store_cv_values=True)
LassoCV.fit(x_train,y_train)
y_test_pred_lasso=lr.predict(x_test)
y_train_pred_lasso=lr.predict(x_train)

from sklearn.metrics import r2_score
print('The r2 score of LinearRegresssion on test is',r2_score(y_test,y_test_pred_lr))
print('The r2 score of RidgeCV on test is',r2_score(y_test,y_test_pred_lr))
print('The r2 score of LassoCV on test is',r2_score(y_test,y_test_pred_lr))